package com.dminvoicemanager.tabs;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.dminvoicemanager.DM_InvoiceNew;
import com.dminvoicemanager.DM_InvoiceSalesOrder;
import com.dminvoicemanager.DM_Retailer_Master;
import com.dminvoicemanager.R;

public class DM_RetOutstanding_Information extends Activity {
	TextView txtHeading;
	private static final String TAG = null;
	SQLiteDatabase mydb;
	String TagIdRetailer, TxnDate;
	private String DBNAME = "dminvoicemanager.db";
	SimpleDateFormat originalFormat = new SimpleDateFormat(
			"mm/dd/yyyy hh:mm:ss");
	SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
	Date date;
	String TagId;
	String TagMain;
	public void btnBack(View view) {
		if (TagId.equals("TAB")) {
			Intent myIntent = new Intent(view.getContext(),
					DM_Retailer_Master.class);
			myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivityForResult(myIntent, 0);
			finish();
		} else if(TagMain.equals("SI")){
			Intent myIntent = new Intent(view.getContext(), DM_InvoiceNew.class);
			myIntent.putExtra("TAGID", "5");
			myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivityForResult(myIntent, 0);
			finish();
		} else if(TagMain.equals("SO")){
			Intent myIntent = new Intent(view.getContext(), DM_InvoiceSalesOrder.class);
			myIntent.putExtra("TAGID", "5");
			myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivityForResult(myIntent, 0);
			finish();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_retoutstanding_information);

		Bundle bundle = getIntent().getExtras();
		TagId = bundle.getString("TAGID");

		if (TagId.equals("TAB")) {

		} else {
			String[] ToLocDetails = TagId.split("/");
			TagMain = ToLocDetails[0]; // fromLocCode
			TagIdRetailer = ToLocDetails[1]; // fromLocName
			
			//TagIdRetailer = TagId;
		}
		txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Outstanding Information");
		callCurrentRetailer();
		DisplayRetailerList();
	}

	private void callCurrentRetailer() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentRetailer = null;
		try {
			String query = "SELECT * from FRetailScrSum";
			curCurrentRetailer = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentRetailer != null) {
			if (curCurrentRetailer.getCount() > 0) {
				while (curCurrentRetailer.moveToNext()) {
					TagIdRetailer = curCurrentRetailer.getString(1);
				}
			}
		}

	}

	private void DisplayRetailerList() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dmretaileroutstandlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2", "coldes_3" };
		int[] totrdes = new int[] { R.id.retoutreference, R.id.retoutdate,
				R.id.retoutamount };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curListRetailer = null;
		try {
			String query = "SELECT RefNo,TxnDate,TotBal from FDDbNote Where DebCode = '"
					+ TagIdRetailer + "' AND TotBal !='0'";
			curListRetailer = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curListRetailer != null) {
			if (curListRetailer.getCount() > 0) {
				while (curListRetailer.moveToNext()) {
					String strRefNo = curListRetailer.getString(0);
					String strTxnDate = curListRetailer.getString(1);
					TxnDate = strTxnDate.substring(0, 10);
					String strBAmt = curListRetailer.getString(2);

					/*
					 * String retailerDetSum = strDebCode + "/" + strDebName +
					 * "/" + strTownCode + "/" +strTownName;
					 */

					HashMap<String, String> maptrdes = new HashMap<String, String>();
					maptrdes.put("coldes_1", strRefNo);
					maptrdes.put("coldes_2", TxnDate);
					maptrdes.put("coldes_3", strBAmt);
					fillMapstrdes.add(maptrdes);

				}
				// fill in the grid_item layout
				SimpleAdapter adaptertrdes = new SimpleAdapter(this,
						fillMapstrdes,
						R.layout.dm_retoutstanding_information_raw, fromtrdes,
						totrdes);
				lvtypedes.setAdapter(adaptertrdes);
			}
		}
		curListRetailer.close();
		mydb.close();

	}

	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
